package com.atguigu.srb.core.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.pojo.dto.ExcelDictDTO;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

@Slf4j
public class ExcelDictDTOListener extends AnalysisEventListener<ExcelDictDTO> {


    DictMapper dictMapper;

    public static Long count = 0l;

    ArrayList<ExcelDictDTO> excelDictDTOS = new ArrayList<>();

    public ExcelDictDTOListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    @Override
    public void invoke(ExcelDictDTO excelDictDTO, AnalysisContext analysisContext) {
        excelDictDTOS.add(excelDictDTO);
        count++;
        if(count%5 == 0 ){
            System.out.println("将五条数据插入数据库");
            System.out.println(excelDictDTOS);
            dictMapper.insertBatch(excelDictDTOS);
            excelDictDTOS.clear();
        }
        System.out.println("==========="+count);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("全部完成,后将剩余数据插入数据库");
        System.out.println(excelDictDTOS);
        dictMapper.insertBatch(excelDictDTOS);
    }
}
